/*********************************************************************** 
 *	 
 *     Copyright: 2006-2088, BAINA Technologies Co. Ltd.
 *     Classname: NetCategoryActivity.java
 *     Author:    yuewang
 *     Description:    Use Broadcast to watch network status.
 *     
 *     History:
 *         1.  Date:    2010-10-25
 *             Author:    yuewang
 *             Modifycation:    create the activity
 *
 ***********************************************************************/
package com.sdk.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkStatusWatcher extends BroadcastReceiver {

	private boolean mIsNetworkAvailable;
	private StatusListener mStatusListener;

	public NetworkStatusWatcher() {

	}

	/**
	 * Checks whether the device is able mTo connect mTo the network
	 * 
	 * @param mContext
	 *            please invoke getApplicationContext() to pass context. In case
	 *            of context leak.
	 * @return true if network is available. false otherwsie
	 */
	public static boolean isNetworkAvailable(final Context context) {
		final ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			final NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i = i + 1) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	public void setStatusListener(final StatusListener statusListener) {
		mStatusListener = statusListener;
	}

	@Override
	public final void onReceive(final Context context, final Intent intent) {
		final String action = intent.getAction();
		if ("android.intent.action.SERVICE_STATE".equals(action)) {
			mIsNetworkAvailable = isNetworkAvailable(context);
			if (null != mStatusListener) {
				mStatusListener.onNetworkStatusChange(mIsNetworkAvailable);
			}
		}
	}

	public void register(final Context context) {
		final IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction("android.intent.action.AIRPLANE_MODE_CHANGED");
		intentFilter.addAction("android.intent.action.SERVICE_STATE");
		context.registerReceiver(this, intentFilter);
	}

	public void unregister(final Context context) {
		context.unregisterReceiver(this);
	}

	/**
	 * manual scan network
	 * 
	 * @param context
	 * @return true if network is availiable, false otherwise.
	 */
	public boolean scanNetwork(final Context context) {
		mIsNetworkAvailable = isNetworkAvailable(context);
		if (null != mStatusListener) {
			mStatusListener.onNetworkStatusChange(mIsNetworkAvailable);
		}
		return mIsNetworkAvailable;
	}

	/**
	 * Watch the network status.
	 * 
	 */
	public interface StatusListener {
		public void onNetworkStatusChange(boolean isAvailable);
	}

}
